﻿using Max.PH4.Shoppgin.Admin.Domain;
using Max.PH4.Shoppgin.Admin.DTO;
using Max.PH4.Shoppgin.Admin.IRespository;
using Max.PH4.Shoppgin.Admin.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Max.PH4.Shoppgin.Admin.Service
{
    public class SKUService : ISKUService
    {

        ISPURespository _spu;
        ISKURespository _sku;
        ISKUImageRespository _image;
        ISPUSpecRespository _spec;
        IUnitOfWork _unitOfWork;
        public SKUService(ISPURespository spu, ISKURespository sku, ISKUImageRespository image, ISPUSpecRespository spec, IUnitOfWork unitOfWork)
        {
            _spu = spu;
            _sku = sku;
            _image = image;
            _spec = spec;
            _unitOfWork = unitOfWork;
        }

        #region 添加商品

        //添加商品：SPU、SKU、SKU图片、SPU的规格
        public void AddSKU(ProductInputDto dto)
        {
            /*
             * 点击前台的那个提交按钮，会把界面上的信息，全都提交到这个方法里。所以：
             * 1、需要添加SPU表信息             --- 只有一条SPU信息
             * 2、需要添加SKU表信息             --- 会有多条SKU信息
             * 3、需要添加SKU对应的图片信息     --- 每个SKU会有多条图片            
             * 4、需要添加SPU对应的规格信息     --- 会有多条规格信息(及规格值）
             * 
             * **/

            //开启事务
            using (var tran = _unitOfWork.GetMyDbContext().Database.BeginTransaction()) 
            {
                try
                {
                    #region 业务操作
                    //前台的SPU信息
                    SPUEntity spu = new SPUEntity()
                    {
                        Id = Guid.NewGuid(),
                        BrandId = dto.BrandId,
                        CategoryId = dto.CategoryId,
                        Description = dto.Description
                    };

                    List<SKUEntity> skuList = new List<SKUEntity>();
                    List<SKUImageEntity> skuImageList = new List<SKUImageEntity>();
                    //前台的SKU信息
                    dto.Skus.ForEach(x =>
                    {
                        SKUEntity _s = new SKUEntity
                        {
                            Id = Guid.NewGuid(),
                            InPrice = x.InPrice,
                            MarketPrice = x.MarketPrice,
                            SalePrice = x.SalePrice,
                            Specs = x.Specs,
                            SPUId = spu.Id,
                            Stock = x.Stock,
                            ValidStock = x.Stock
                        };
                        skuList.Add(_s);

                        //前台SKU对应的图片列表信息
                        x.ImagePath.ForEach(p =>
                        {
                            SKUImageEntity _g = new SKUImageEntity()
                            {
                                SKUId = _s.Id,
                                Url = p
                            };
                            skuImageList.Add(_g);
                        });
                    });



                    List<SPUSpecEntity> skuSpecList = new List<SPUSpecEntity>();
                    //前台的SPU对应的规格信息
                    dto.Specs.ForEach(x => {
                        SPUSpecEntity _ss = new SPUSpecEntity()
                        {
                            SPUId = spu.Id,
                            SpecName = x.SpecName,
                            SpecValue = x.SpecValue
                        };
                        skuSpecList.Add(_ss);
                    });

                    //上面的数据添加到数据库
                    _spu.Insert(spu);
                    skuList.ForEach(k =>
                    {
                        _sku.Insert(k);
                    });
                    skuImageList.ForEach(k =>
                    {
                        _image.Insert(k);
                    });
                    skuSpecList.ForEach(k =>
                    {
                        _spec.Insert(k);
                    });
                    #endregion

                    _unitOfWork.SaveChanges();
                    tran.Commit(); //成功，提交事务
                }
                catch (Exception)
                {
                    tran.Rollback();  //失败，回滚事务
                }
            }
        }

        #endregion

        public int Delete(SKUEntity entity)
        {
            throw new NotImplementedException();
        }

        public int Delete(object id)
        {
            throw new NotImplementedException();
        }

        public List<SKUEntity> Query()
        {
            throw new NotImplementedException();
        }

        public IQueryable<SKUEntity> Query(string name)
        {
            throw new NotImplementedException();
        }

        public IQueryable<SKUEntity> Query(string name, int pageIndex, int pageSize, out int total)
        {
            throw new NotImplementedException();
        }

        public int Update(SKUEntity entity)
        {
            throw new NotImplementedException();
        }

        public int Update(List<SKUEntity> entities)
        {
            int result = 1;
            using (var tran = _unitOfWork.GetMyDbContext().Database.BeginTransaction())
            {
                try
                {
                    entities.ForEach(k => {
                        k.IsDeleted = true;
                    });

                    _sku.Update(entities);
                    _unitOfWork.SaveChanges();

                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    result = -1;
                    throw;
                }
            }
            return result;
        }
    }
}
